<template>
  <div>
    <el-dialog
      :title="form.uid ? '编辑' : '添加'"
      :visible.sync="usercase.flag"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form.roleid" placeholder="请选择角色">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员名称" :label-width="formLabelWidth">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="管理员密码" :label-width="formLabelWidth">
          <el-input
            v-model="form.password"
            :placeholder="form.uid ? '不填写则不修改' : ''"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="管理员名称" :label-width="formLabelWidth">
          <el-switch v-model="form.status" active-value="1" inactive-value="2">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="usercase.flag = false">取 消</el-button>
        <el-button type="primary" @click="adduser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["usercase", "paswrd"],
  data() {
    return {
      formLabelWidth: "120px",
      form: {
        //数据表单
        username: "",
        password: "",
        status: 2,
        roleid: "",
      },
      rolelist: [],
    };
  },
  computed: {},
  created() {
    this.getrolelist();
  },
  mounted() {},
  watch: {
    usercase: {
      deep: true,
      handler: function (newval, oldval) {
        // console.log(newval);
        if (newval.flag == false) {
          this.form = {
            username: "",
            password: "",
            status: 2,
            roleid: "",
          };
        }
      },
    },
  },
  methods: {
    getrolelist() {
      // 获取角色列表
      this.$api.getrolelist().then((res) => {
        this.rolelist = res.data.list;
      });
    },
    adduser() {
      // 确认事件
      if (this.form.username == "") {
        this.$message.error("请填写正确的信息");
      } else {
        if (this.form.password == "") {
          this.form.password = this.paswrd
        };
        (this.form.uid
          ? this.$api.postuseredit(this.form)
          : this.$api.postuseradd(this.form)
        ) // 编辑管理员&&添加管理员
          .then((res) => {
            this.$message({
              message: "操作成功",
              type: "success",
            });
            this.$emit("getuserList");
          });
          console.log(this.form);
        this.usercase.flag = false;
        this.$emit("gettotal");
      }
    },
  },
  components: {},
};
</script>

<style scoped lang="scss">
</style>
